package com.ra.landlord;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.cswish.annotation.Rest;
import com.googlecode.cswish.exception.ApplicationException;
import com.googlecode.cswish.struts.spring.GenericJPAService;
import com.googlecode.cswish.struts.spring.GenericService;

/**
 * 地址服务
 * 
 * @author colin
 * @date 2014年11月20日
 *
 */
@Service
public class BuildingFloorService {
	
	@Resource
	private GenericService genericService;
	
	@Resource
	private GenericJPAService genericJPAService;
	
	@Resource
	private LandlordUserService landlordUserService;
	
	@Rest(type="search")
	public List<BuildingFloor> searchBuildingFloor(BuildingFloor model) {
		LandlordUser landlordUser = this.landlordUserService.getLoginUser();
		model.setLandlordUser(landlordUser.getOwnerUser());
		return genericService.searchAll(model).getData();
	}
	
	@Transactional
	@Rest(type="_delete")
	public boolean deleteBuildingFloor(List<BuildingFloor> buildingFloors) {
		boolean result = true;
		
		StringBuffer sbMessage = new StringBuffer();
		if(buildingFloors != null){
			for (BuildingFloor buildingFloor : buildingFloors) {
				try {
					genericJPAService.delete(buildingFloor);
				} catch (Exception e) {
					sbMessage.append(buildingFloor.getName()).append(",");
					result = false;
				}
			}
		}
		if (!result) {
			throw new ApplicationException("楼房删除失败,"+sbMessage.toString()+"这些楼层可能还存在房间未删除");
		}
		return result;
	}
}
